import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: '',
		webSocket: null
	},
	mutations: {
		SET_SOCKET: (state, webSocket) => {
			state.webSocket = webSocket
		},
		login(state, provider) {
			if (provider && provider != undefined) {
				state.hasLogin = true;
			}
			state.userInfo = provider;
			uni.setStorage({ //缓存用户登陆状态
				key: 'userInfo',
				data: provider.userInfo
			})

		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = '';
			uni.removeStorage({
				key: 'userInfo'
			})
		}
	},
	actions: {
		// 设置webSocket
		setWebSocket({
			commit
		}, data) {
			commit('SET_SOCKET', data)
		},
	}
})

export default store